package main

import (
	"gorm.io/gorm"
	"main.go/model"
	"strconv"
	"strings"
)

func Get_data(in_house model.In_house, out_house model.Out_house) int {
	data1 := in_house.In_house_time
	data2 := out_house.Out_house_time
	var s1 []string
	var s2 []string
	s1 = strings.Split(data1, "-")
	s2 = strings.Split(data2, "-")
	var num1 int
	var num2 int
	var num int
	i := 0
	result := 0
	for i < 3 {
		num1, _ = strconv.Atoi(s1[i])
		num2, _ = strconv.Atoi(s2[i])
		num = num2 - num1
		if i == 0 {
			result = result + num*365
		} else if i == 1 {
			result = result + num*30
		} else {
			result = result + num*1
		}
		i = i + 1
	}
	return result
}

func Get_tun(in_house model.In_house, out_house model.Out_house) int {
	return in_house.In_house_tun - out_house.Out_house_tun
}

func Calculate(db *gorm.DB, in_house model.In_house, out_house model.Out_house) int {
	var house model.House
	db.Where("house_id = ?", in_house.In_house_house_id).First(&house)
	data := Get_data(in_house, out_house)
	tun := Get_tun(in_house, out_house)
	if data <= 14 {
		return 0
	}
	rate := house.House_rate
	result := rate * data * tun
	return result
}
